﻿namespace Ocelot.Admin.Api.Application
{
    public class RouteMapper : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            config.ForType<Route, RouteDto>()
                .Map(dest => dest.DelegatingHandlers,
              src => string.IsNullOrWhiteSpace(src.DelegatingHandlers) ? new List<string>() : src.DelegatingHandlers.Split(',', '，').ToList())
                .Map(dest => dest.RatelimitWhitelist,
              src => string.IsNullOrWhiteSpace(src.RatelimitWhitelist) ? new List<string>() : src.RatelimitWhitelist.Split(',', '，').ToList())
                .Map(dest => dest.SecurityIpallowedlist,
              src => string.IsNullOrWhiteSpace(src.SecurityIpallowedlist) ? new List<string>() : src.SecurityIpallowedlist.Split(',', '，').ToList())
                .Map(dest => dest.SecurityIpblockedlist,
              src => string.IsNullOrWhiteSpace(src.SecurityIpblockedlist) ? new List<string>() : src.SecurityIpblockedlist.Split(',', '，').ToList())
                .Map(dest => dest.AuthenticationAllowedscopes,
              src => string.IsNullOrWhiteSpace(src.AuthenticationAllowedscopes) ? new List<string>() : src.AuthenticationAllowedscopes.Split(',', '，').ToList())
                .Map(dest => dest.UpstreamHttpMethod,
              src => string.IsNullOrWhiteSpace(src.UpstreamHttpMethod) ? new List<string>() : src.UpstreamHttpMethod.Split(',', '，').ToList());
             
            config.ForType<RouteDto, Route>()
              .Map(dest => dest.DelegatingHandlers,
            src => src.DelegatingHandlers != null ? string.Join(',', src.DelegatingHandlers) : "")
              .Map(dest => dest.RatelimitWhitelist,
            src => src.RatelimitWhitelist != null ? string.Join(',', src.RatelimitWhitelist) : "")
              .Map(dest => dest.SecurityIpallowedlist,
            src => src.SecurityIpallowedlist != null ? string.Join(',', src.SecurityIpallowedlist) : "")
              .Map(dest => dest.SecurityIpblockedlist,
            src => src.SecurityIpblockedlist != null ? string.Join(',', src.SecurityIpblockedlist) : "")
              .Map(dest => dest.AuthenticationAllowedscopes,
            src => src.AuthenticationAllowedscopes != null ? string.Join(',', src.AuthenticationAllowedscopes) : "")
              .Map(dest => dest.UpstreamHttpMethod,
            src => src.UpstreamHttpMethod != null ? string.Join(',', src.UpstreamHttpMethod) : "");

        }
    }
}